// src/navigation/navigationUtils.ts
import { createNavigationContainerRef } from '@react-navigation/native';
import { RootStackParamList } from './types';

export const navigationRef = createNavigationContainerRef<RootStackParamList>();

export function navigate(name: keyof RootStackParamList, params?: any) {
  if (navigationRef.isReady()) {
    navigationRef.navigate(name as any, params);
  }
}

export function goBack() {
  if (navigationRef.isReady() && navigationRef.canGoBack()) {
    navigationRef.goBack();
  }
}

export function reset(name: keyof RootStackParamList) {
  if (navigationRef.isReady()) {
    navigationRef.reset({
      index: 0,
      routes: [{ name: name as any }],
    });
  }
}

// 深度链接配置
export const linking = {
  prefixes: ['aitool://', 'https://aitool.app'],
  config: {
    screens: {
      Auth: {
        screens: {
          Login: 'login',
          Register: 'register',
          ForgotPassword: 'forgot-password',
          ResetPassword: 'reset-password/:email/:code',
        },
      },
      Main: {
        screens: {
          MainTabs: {
            screens: {
              Home: 'home',
              Chat: 'chat',
              Translate: 'translate',
              Voice: 'voice',
              Profile: {
                screens: {
                  ProfileMain: 'profile',
                  Subscription: 'subscription',
                },
              },
            },
          },
          ChatDetail: 'chat/:conversationId',
          History: 'history',
          Favorites: 'favorites',
        },
      },
    },
  },
};